package org.danysoft.encoding_converter.core;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

public class ByteBufferConverter {

  public static ByteBuffer convertBuffer(ByteBuffer buffer, Charset inputCharset, Charset outputCharset) throws IOException {

    assert 
         buffer != null
      && inputCharset != null
      && outputCharset != null;
    
    CharsetDecoder inputDecoder = inputCharset.newDecoder();
    CharsetEncoder outputEncoder = outputCharset.newEncoder();
    CharBuffer cb = inputDecoder.decode(buffer);
      
    return outputEncoder.encode(cb);
  }

}
